<?php

use backend\modules\tool\DataSource\Log\PidRecord;
use yii\helpers\Html;
use yii\helpers\Url;
use dsj\components\grid\ResponsiveGridView;
use yii\widgets\ActiveForm;
use backend\modules\tool\models\DataBaseBack;

/* @var $this yii\web\View */
/* @var $dataProvider yii\data\ActiveDataProvider */
/* @var $model backend\modules\tool\models\DataBaseBack */
\backend\modules\tool\Assets\SeachBundle::register($this);
\backend\modules\tool\Assets\BaseBundle::register($this);
\backend\modules\tool\Assets\VueBundle::register($this);
$this->title = '整库备份';
$this->params['breadcrumbs'][] = $this->title;
$log=Url::to(["/tool/log"]);
function get_config(){
    $data=\backend\modules\tool\models\SqlConfig::find()
        ->select(new \yii\db\Expression("id,source_name"))
        ->asArray()
        ->all();
    $data=\backend\modules\tool\helpers\ArrayHelper::array_parse_key_value($data,"id","source_name");
    return $data;
}
$source_config=get_config();
$js=<<<js
new Vue({
el:"#app",
data:{
    command:[]
},
methods:{
        code(text){
			        return "<pre><code>"+text+"</code></pre>";
			    },
    log(id){
        var self=this;
         axios.get('{$log}'+"&id="+id).then(function(res) {
           var data=res.data.data.split("=======================>");
           var log=[];
           for(var i of data){
               if(i.trim()!=""){
                   log.push(i)
               }
           }
           self.command=log;
           // if(self.layer_index!=null){
           //     layer.close(self.layer_index);
           // }
           setTimeout(function() {
             self.layer_index=layer.open({
              type: 1,
              shade: false,
              title: false, //不显示标题
              content: $('#log'), //捕获的元素，注意：最好该指定的元素要存放在body最外层，否则可能被其它的相对元素所影响
              area:["1200px","800px"],
              end:function (){
                  $('#log').css("display","none");
              }
           });
           },1);
           
         }).catch(function(err) {
           
         })
    }
}
});
js;
$this->registerJs($js);
?>
<div class="DataBaseBack-index" id="app">
    <div style="display: flex">
        <?= Html::button('新增节点', ['class' => 'btn btn-success data-create','url' => Url::to(['create'])]) ?>
        <?php $back_url=Url::to(['/tool/node']) ?>
        <button type="button" class="btn btn-primary" onclick="window.location.href='<?=$back_url?>'">返回SQL节点</button>
        <div style="max-height:800px;overflow-y: scroll;left: 0;width: 100%;display: none;padding: 20px;" id="log">
            <div class="code" v-for="i in command" style="">
                <pre v-html="code(i)"></pre>
            </div>
        </div>
    </div>
<?php $form = ActiveForm::begin(['method'=>'get']); ?>
<?php ActiveForm::end(); ?>
    <?= \yii\grid\GridView::widget([
        'dataProvider' => $dataProvider,
        'columns' => [
            ['class' => 'yii\grid\SerialColumn'],
            [
                'attribute' => 'source_database',
                'label' => '目标数据库',
                'value' => function($model)use($source_config){
                    return $source_config[$model->source_database];
                }
            ],
            'last_run',
            'node_name',
            ['class' => \backend\modules\tool\helpers\ResponsiveActionColumn::class,
                'template'=> '{add-task} {create} {update} {view} {delete} {switch} {log}',
                'buttons' => [
                    'parse-sql' => function ($url, $model, $key) {
                        return "<button type='button' @click='get_data({$model->id})' class='btn btn-sm btn-success'>查看数据</button>";
                    },
                    'add-task'=>function($url, $model, $key){
                        return sprintf("<button type=\"button\" class=\"btn btn-primary btn-sm data-view\" title=\"查看\" aria-label=\"查看\" data-pjax=\"0\" url='%s'>节点运行配置</button>",Url::to(["add-task"])."&task_id=".$model->id);
                    },
                    'switch'=>function($url, $model, $key){
                        $class_path=str_replace("\\","_",\backend\modules\tool\Job\DataBaseBackNode::class);
                        if(\backend\modules\tool\DataSource\Log\PidRecord::SingleTon($model->id.$class_path)->IsRun()){
                            return "<button type=\"button\" class=\"btn btn-sm btn-success\">运行中</button>";
                        }else{
                            return "<button type=\"button\" class=\"btn btn-sm btn-secondary\">未运行</button>";
                        }
                    },
                    "view"=>function($url, $model, $key){
                        return "";
                    },
                    "log"=>function($url, $model, $key){
                        $id=DataBaseBack::getName($model->id);
//                        $model->id=$id;
                        return "<button type=\"button\" class=\"btn btn-sm btn-info\" @click=\"log('$id')\">日志</button>";
                    }
                ]],
        ],
    ]); ?>
</div>
